import axios from 'axios';
import qs from 'qs';
import tool from '@/utils/tool'
import store from '@/store'
import Config from '@/config'

//Axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';


const request = function (obj){
  return new Promise((resolve, reject)=>{
    let app = obj.app ? obj.app : Config.app;
    let version = obj.version ? obj.version : Config.version;
    let congtroller = obj.c;
    let method = obj.method ? obj.method : 'POST';
    let headers = obj.headers ? obj.headers : {};
    //headers['Access-Control-Max-Age'] = 1800;
    if (store.getters.userInfo.user_token) headers['User-Token'] = store.getters.userInfo.user_token;
    if (!obj.data) obj.data = {};
		obj.data['platform_app_id'] = store.getters.appInfo.platform_app_id;
    obj.data['platform_user_id'] = store.getters.appInfo.platform_user_id;
    obj.data['platform_app_user_id'] = store.getters.appInfo.platform_app_user_id;
    if (obj.loading) tool.loading(obj.loading);
    let lang = sessionStorage.getItem('lang');
    lang = lang ? lang : Config.defaultLang;
    let url = obj.url ? obj.url : Config.requestUrl + '/'+app+'/'+version+'/'+congtroller + '?lang='+lang;
    return axios({
      url: url,
      data: qs.stringify(obj.data),
      method: method,
      headers:headers
    }).then(res=>{
      if (obj.loading)  tool.loading();
      if (res.data.type == 'ok'){
        resolve(res.data.data);
      } else if (res.data.type == 'fail') {
        switch (obj.error) {
          case 'alert':
            tool.alert(res.data.msg);
            break;
          case 'toast':
            tool.toast(res.data.msg);
            break;
          case 'errorToast':
              tool.errorToast(res.data.msg);
              break;
          case 'toastBack':
            tool.toastBack(res.data.msg);
            break;
          case 'alertBack':
            tool.alertBack(res.data.msg);
            break;
          default :
            tool.errorToast(res.data.msg);
        }
        if (res.data.errorCode == 20002 || res.data.errorCode==20001){
          setTimeout(()=>{
            store.dispatch('user/resetToken').then(() => {
              location.reload()
            })
          },1000);
          
        }
        if (!obj.error) reject(res.data.msg);
        
      } else {
        tool.errorToast('遇到了点小问题');
        if (obj.reject) reject();
      }
    }).catch(error=>{
      if (obj.loading)  tool.loading();
      switch (obj.error) {
        case 'alert':
          tool.alert('请求错误');
          break;
        case 'toast':
          tool.toast('请求错误');
          break;
        case 'errorToast':
            tool.errorToast('请求错误');
            break;
        case 'toastBack':
          tool.toastBack('请求错误');
          break;
        case 'alertBack':
          tool.alertBack('请求错误');
          break;
        default :
          tool.errorToast('请求错误');
      }
      if (!obj.error) reject('请求错误');
      
    });
  });
}

export default request
